package level4.display.effects
{
	import flash.display.DisplayObject;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	
	import level4.display.filters.Filters;

	public class Effects
	{
		public function Effects()
		{
		}
		
		/**
		 * 晃动效果
		 * */
		public static function rockEffect(do2d:DisplayObject):void{
			if(!do2d)
				return;
			
			var originalPosObj:Object = {x:do2d.x,y:do2d.y};
			var count:int = 10;
			var vx:Number = 0;
			var vy:Number = 0;
			var friction:Number = .9;
			//布朗运动
			var timerId:int = setInterval(function():void{
				if(count < 0){
					clearInterval(timerId);
					do2d.x = originalPosObj.x;
					do2d.y = originalPosObj.y;
				}
				
				sinMove(do2d);
				count --;
			},33);
			
			///布朗运动
			function random(do2d:DisplayObject):void{
				vx += Math.random() * 4 -2;
				vy += Math.random() * 4 -2;
				vx *= friction;
				vy *= friction;
				
				do2d.x += vx;
				do2d.y += vy;
			}
			//正选运动
			var angle:Number = 0;
			var step:Number = 90/180 * Math.PI;
			function sinMove(do2d:DisplayObject):void{
				angle += step;
				var range:Number = Math.sin(angle) * 10;
				do2d.y += range;
			}
		}
		/**
		 * 闪动效果
		 * 
		 * */
		public static function flashGlowEffect(do2d:DisplayObject,intervalSeconds:int,totalCount:int,color:uint):void{
			if(do2d){
				var count:int = 0;
				var timerId:int = setInterval(function():void{
					
					if(do2d.filters.length == 0){
						do2d.filters = [Filters.glowFilter(color,1)];
					}else{
						do2d.filters = [];
					}
					
					count ++;
					
					if(count >= totalCount){
						clearInterval(timerId);
					}
				},intervalSeconds);
			}
		}
		
	}
}